<template>
  <div>
    <!-- data：表格的数据 -->
    <!-- row-key：用来渲染树形数据 -->
    <!-- default-expand-all：默认展开所有 -->
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" border>
      <!-- prop:数据的字段 -->
      <!-- label:数据的文字 -->
      <!-- sortable：排序 -->
      <el-table-column prop="firstcatename" label="一级分类" width="180">
      </el-table-column>
      <el-table-column prop="secondcatename" label="二级分类" width="180">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="180">
      </el-table-column>
      <el-table-column label="商品图片" width="120">
        <template slot-scope="scope">
          <img v-if="scope.row.img" :src="$host + scope.row.img" width="100" />
        </template>
      </el-table-column>
      <el-table-column label="是否新品" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="isnewChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="ishotChange"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-show="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-show="scope.row.status === 2">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.uid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- size-change：每页的条数修改时触发的函数 -->
    <!-- current-change：当前页码变化时触发的函数 -->
    <!-- current-page：当前页码数 -->
    <!-- page-sizes：每页条数的下拉选项 -->
    <!-- page-size：每页显示多少条 -->
    <!-- layout：显示的控件内容和顺序 -->
    <!-- total：数据的总数 -->
    <!-- 分页的逻辑：
      关键变量：1.total（数据总条数，后端知道）2.pageSize 每页显示多少条（前端知道
              3. 总页数=total/pageSize 有余数就向上取整  4.currentPage 页码数
     -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper, total"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getGoodsCountFn, getGoodsListFn } from "../../apis";
export default {
  data() {
    return {
      // 表格的数据
      tableData: [],
      // 当前页码
      currentPage: 1,
      // 每页显示几条
      pageSize: 10,
      // 数据总条数
      total: 1,
    };
  },
  created() {
    // 发请求，获取菜单
    this.getGoodsCount();
  },
  methods: {
    async getGoodsCount() {
      let res = await getGoodsCountFn();
      this.total = res.list[0].total;
      this.getUserList();
    },
    // 根据分页获取具体数据  size和page
    async getUserList() {
      let res = await getGoodsListFn({
        size: this.pageSize,
        page: this.currentPage,
      });
      this.tableData = res.list;
    },
    edit(data) {
      // 子传父，打开弹窗并将data填入弹窗
      this.$emit("edit", data);
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 真的删除
          this.trueDel(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async trueDel(id) {
      let res = await delRoleFn({ id: id });
      if (res.code === 200) {
        // 更新数据
        this.tableData = res.list;
        // 提示成功
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      }
    },
    // 当前页码变化时触发的函数
    handleCurrentChange(val) {
      // 修改页码数
      this.currentPage = val;
      this.getUserList();
    },
    // 通过开关修改是否新品或是否热卖
    isnewChange(val) {
      // 删除多余的数据，然后发请求修改商品信息
      this.sendData(val)
    },
    ishotChange(val) {
      // 删除多余的数据，然后发请求修改商品信息
      this.sendData(val)
    },
    async sendData(val) {
      let data = val;
      delete data.number;
      delete data.firstcatename;
      delete data.secondcatename;
      let fd = new FormData();
      for (const key in data) {
        fd.append(key, data[key])
      }
      let res = await xxxFn(fd)
    }
  },
};
</script>

<style>
</style>